/*Hdip Project 2013-14, Student Name:- Chris Pembroke,  Student Number:- x00115839*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Exercise1Project.Models.Validation;
using System.Web.Mvc;
using System.Text.RegularExpressions;

namespace Exercise1Project.Models
{
    public class CustomerDetailBusinessLogic : ICustomerDetailBusinessLogic
    {

        private IValidationDictionary _validationDictionary;  // implements validation logic via model state wrapper class
        private ICustomerDetailRepository _repository; // implements customer detail repository which interacts with database logic

        public CustomerDetailBusinessLogic(IValidationDictionary validationDictionary) : this(validationDictionary, new CustomerDetailsDatabaseLogic())
        {

        }

        public CustomerDetailBusinessLogic (IValidationDictionary validationDictionary, ICustomerDetailRepository repository)
        {
            _validationDictionary = validationDictionary;
            _repository = repository;
        }

        //Validation Business Logic
        public bool ValidateUser(User userToValidate)
        {
            if (userToValidate.Name == null || userToValidate.Name.Trim().Length == 0 || userToValidate.Name.Trim().Length > 20)       // null or trim().length checks if null or empty
                _validationDictionary.AddError("Name", "Required Field! with less than 20 characters");

            if (userToValidate.Address == null || userToValidate.Address.Trim().Length == 0 || userToValidate.Address.Trim().Length > 80)
                _validationDictionary.AddError("Address", "Required Field! with less than 80 characters");

            if (userToValidate.Email == null || userToValidate.Email.Trim().Length == 0 || (userToValidate.Email.Trim().Length > 0 && !Regex.IsMatch(userToValidate.Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$")))
                _validationDictionary.AddError("Email", "Required Field! must be valid email");

            if (userToValidate.Phone == null || userToValidate.Phone.Trim().Length == 0 || (userToValidate.Phone.Trim().Length > 0 && !Regex.IsMatch(userToValidate.Phone, @"((\(d{3}\) ?)|(d{3}-))?\d{3}-\d{4}")))
                _validationDictionary.AddError("Phone", "Required Field! of form ***-****");

            return _validationDictionary.IsValid;
        }

        //Database Business Logic
        public User GetCustomerDetailsByCustomerId(string id)
        {
              return  _repository.GetCustomerDetailsByCustomerId(id);
        }

        public bool UpdateCustomerDetails(User userToUpdate)
        {
            //Validation
            if (!ValidateUser(userToUpdate))
                return false;

            //Database
            try
            {
                _repository.UpdateCustomerDetails(userToUpdate);
            }
            catch
            {
                return false;
            }
            return true;
        }


    }
}
